package com.compass.doctor.ui.prescription.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.LayoutHistoryPrescriptionItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseRecyclerViewAdapter
import com.compass.framework.ext.onClick
import com.compass.framework.model.Diagnosis
import com.compass.framework.model.Drug
import com.compass.framework.model.TemplateDetails
import com.compass.framework.utils.DateUtils

/**
 * Description： 历史模板adapter
 * Date:2023/12/19 10:58
 */
class HistoryTemplateAdapter :
    BaseRecyclerViewAdapter<TemplateDetails, LayoutHistoryPrescriptionItemBinding>() {


    private var onCustomBottomClick: OnCustomButtonClick? = null
    fun setBottomCallBack(onChatBottomClick: OnCustomButtonClick) {
        this.onCustomBottomClick = onChatBottomClick
    }

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): LayoutHistoryPrescriptionItemBinding {
        return LayoutHistoryPrescriptionItemBinding.inflate(layoutInflater, parent, false)
    }

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<LayoutHistoryPrescriptionItemBinding>,
        item: TemplateDetails?,
        position: Int
    ) {
        if (item == null) return
        holder.binding.apply {
            tvTime.text = DateUtils.conversionTime(item.createTime, DateUtils.FORMAT_TWO)
            tvDiagnosis.text = getDiagnosis(item.diagnosis)
            tvSymptom.text = getDrugs(item.drugs)
            tvTemplateName.text = item.name
            tvDetails.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "details")
            }
            tvDelete.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "delete")
            }
        }
    }


    private fun getDiagnosis(list: MutableList<Diagnosis>?): String {
        list?.let {
            for (item in list) {
                if (item.main) return item.name
            }
        }
        return ""
    }

    private fun getDrugs(list: MutableList<Drug>?): String {
        var listName = ArrayList<String?>()
        list?.let {
            for (item in list) {
                listName.add(item.name)
            }
        }
        return listName.joinToString(separator = "/")
    }
}